//
//  ViewController.m
//  WJSelectedItemsDemo
//
//  Created by 王杰 on 2019/7/22.
//  Copyright © 2019 王杰. All rights reserved.

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
#import <LEEAlert.h>
#import "WJSingleSelectView.h"// 单选
#import "WJMoreSelectView.h"// 多选
#import "WJCollectionViewFlowLayout.h"
#import "WJLiveCourseItemModel.h"
#import <MJExtension.h>
#import "WJCalculateRMBTools.h"

@interface ViewController ()

@property (nonatomic,strong)NSArray<WJLiveCourseItemModel *>*sectionArray;
@property (nonatomic,strong)NSArray<WJLiveCourseItemModel *>*subjectInfo;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // [self createData];
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.view addSubview:button];
    button.backgroundColor = UIColor.redColor;
    [button setTitle:@"Button" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}



#pragma mark - 计算价格 ,没有抽离是因为需要使用模型
//- (NSString *)sumPrice:(NSArray <SelectedListModel*>*)priceArr{
//    NSMutableArray * marray = [[NSMutableArray alloc]init];
//    [priceArr enumerateObjectsUsingBlock:^(SelectedListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        // NSLog(@"选中的选项有%@",obj.price);
//        [marray addObject:obj.price];
//        //NSLog(@"我把车子开到五环,title 拼接%@",marray);
//        // 实时价格刷新变化
//    }];
//    CGFloat sum = [[marray valueForKeyPath:@"@sum.floatValue"] floatValue];
//    return [WJCalculateTools notRounding:sum afterPoint:2];
//}



- (void)click:(UIButton *)button{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"jinyingjie" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    // NSLog(@"-------%@",dict);
    NSDictionary * entity = [dict objectForKey:@"entity"];
    self.sectionArray =  [WJLiveCourseItemModel mj_objectArrayWithKeyValuesArray:[entity objectForKey:@"sectionInfo"]];
    self.subjectInfo =  [WJLiveCourseItemModel mj_objectArrayWithKeyValuesArray:[entity objectForKey:@"subjectInfo"]];

    
    __block LEEAction *tempAction = nil;
    

    WJCollectionViewFlowLayout * layout = [[WJCollectionViewFlowLayout alloc]init];
    WJMoreSelectView * view = [[WJMoreSelectView alloc]initWithFrame:CGRectMake(0, 0,kScreenW, 300)
                                                collectionViewLayout:layout];

    NSMutableArray * marray = [[NSMutableArray alloc]initWithArray:self.sectionArray];

    
    [marray removeObjectAtIndex:0];
    view.dataArray = marray;
    view.title = [entity objectForKey:@"title"];
    __block WJMoreSelectView *blockView = view; //解决循环引用

    view.changeItemsdBlock = ^(NSArray<WJLiveCourseItemModel *> * arr,BOOL selectAll) {
    blockView.itemTitle = [WJLiveCourseItemModel sumPrice:arr];
//    blockView.itemTitle = [SelectedListModel idAppendId:arr withSeparator:@","];

        NSLog(@"多选 -- : %@---%d",arr,selectAll);
        // 控制显示下方购买按钮
        if (arr.count > 0) {
            tempAction.title = @"立即购买";
            tempAction.titleColor = [UIColor redColor];
        }else{
            tempAction.title = @"取消";
            tempAction.titleColor = [UIColor lightGrayColor];
        }
        [tempAction update];
        
    };
    
    
    WJCollectionViewFlowLayout * layout2 = [[WJCollectionViewFlowLayout alloc]init];
    WJSingleSelectView * view2 = [[WJSingleSelectView alloc]initWithFrame:CGRectMake(0, 0,kScreenW, 300)
                                                collectionViewLayout:layout2];
    NSMutableArray * marray2 = [[NSMutableArray alloc]initWithArray:self.sectionArray];
    [marray2 removeObjectAtIndex:0];
    view2.dataArray = marray2;
    view2.title = [entity objectForKey:@"title"];
    
    view2.changeItemsdBlock = ^(WJLiveCourseItemModel *model) {
        //blockView2.itemTitle = [WJLiveCourseItemModel sumPrice:arr];
        //    blockView.itemTitle = [SelectedListModel idAppendId:arr withSeparator:@","];
        
        NSLog(@"单选 -- : %@",model.title);
        // 控制显示下方购买按钮
       
        [tempAction update];
        
    };
    
    
    [LEEAlert actionsheet].config
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeAddCustomView(^(LEECustomView *custom) {
        
        custom.view = view;
    })
    .LeeAddCustomView(^(LEECustomView *custom) {
        
        custom.view = view2;
    })
    .LeeAddAction(^(LEEAction *action) {

        tempAction = action;

        action.title = @"取消";
        

        action.titleColor = [UIColor lightGrayColor];

        action.backgroundColor = [UIColor whiteColor];
    })
    .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderColor(UIColor.cyanColor)
    .LeeActionSheetBottomMargin(0.0f) // 设置底部距离屏幕的边距为0
    .LeeActionSheetBackgroundColor([UIColor yellowColor])
    .LeeCornerRadius(0.0f) // 设置圆角曲率为0
    .LeeConfigMaxWidth(^CGFloat(LEEScreenOrientationType type) {
        
        // 这是最大宽度为屏幕宽度 (横屏和竖屏)
        
        return CGRectGetWidth([[UIScreen mainScreen] bounds]);
    })
    .LeeCloseComplete(^{
       
    })
    .LeeShow();
    
}


@end
